package com.woniu.service.impl;

import com.woniu.entity.Class;
import com.woniu.mapper.ClassMapper;
import com.woniu.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ClassServiceImpl implements ClassService {
    @Resource
    private ClassMapper classMapper;
    @Autowired
    private RedisTemplate<String,Object> redisTemplateInit;
    @Override
    public List<Class> queryClass() {
        List<Class> classes = classMapper.queryClassAll();
        for (Class aClass : classes) {
            redisTemplateInit.opsForHash().put("queryClass",aClass.getClassId()+"",aClass);
        }
        return classes;
    }

    @Override
    public List<Class> queryMy() {
        List<Object> queryMy = redisTemplateInit.opsForHash().values("queryMy");
        ArrayList<Class> classList = new ArrayList<>();
        for (Object cl : queryMy) {
            classList.add((Class) cl);
        }
        return classList;
    }

    @Override
    public List<Class> queryShow() {
        List<Object> queryClass = redisTemplateInit.opsForHash().values("queryClass");
        ArrayList<Class> classes = new ArrayList<>();
        for (Object aClass : queryClass) {
            classes.add((Class) aClass);
        }
        return classes;
    }

    @Override
    public synchronized Class chooseClass(Integer classId) {
        Class aClass =(Class) redisTemplateInit.opsForHash().get("queryMy", classId + "");
        if (aClass != null){
            return null;
        }else {
            Class aClass2 = (Class) redisTemplateInit.opsForHash().get("queryClass", classId+"");
            if (aClass2.getClassToll() < aClass2.getClassSize()) {
//                classMapper.chooseClass(classId);
//                Class aClass1 = classMapper.queryAllByClassId(classId);
                Class aClass1 =(Class) redisTemplateInit.opsForHash().get("queryClass", classId+"");
                aClass1.setClassToll(aClass1.getClassToll()+1);
                redisTemplateInit.opsForHash().put("queryClass", classId+"",aClass1);
                redisTemplateInit.opsForHash().put("queryMy", classId + "", aClass1);
                return aClass1;
            }else {return null;}

        }

//        Class aClass = (Class) redisTemplateInit.opsForHash().get("queryClass", classId+"");
//        //根据课程id查,如果该课程上没有学生id说明没选课，判断是否满课
//        if (aClass.getStudentId() == null){
//            if (aClass.getClassToll() < aClass.getClassSize()){
//                classMapper.chooseClass(classId);
//            }
//        }
    }

    @Override
    public synchronized void deleteClass(Integer classId) {
        HashOperations<String, Object, Object> hash = redisTemplateInit.opsForHash();
        Class aClass1 =(Class) redisTemplateInit.opsForHash().get("queryClass", classId+"");
        aClass1.setClassToll(aClass1.getClassToll()-1);
        redisTemplateInit.opsForHash().put("queryClass", classId+"",aClass1);
        hash.delete("queryMy",classId+"");
    }
}
